package com.xuecheng.content.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.execption.XueChengPlusException;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.model.dto.SaveTeachplanDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.model.po.TeachplanMedia;
import com.xuecheng.content.service.TeachplanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class TeachplanServiceImpl  implements TeachplanService {
    @Autowired
    TeachplanMapper teachplanMapper;

    @Autowired
    TeachplanMediaMapper teachplanMediaMapper;
    @Override
    public List<TeachplanDto> findTeachplanTree(long courseId) {
        return teachplanMapper.treeNodeResultMap(courseId);
    }


    @Transactional
    @Override
    public void saveTeachplan(SaveTeachplanDto teachplanDto) {

        //课程计划id
        Long id = teachplanDto.getId();
        //修改课程计划
        if(id!=null){
            Teachplan teachplan = teachplanMapper.selectById(id);
            BeanUtils.copyProperties(teachplanDto,teachplan);
            teachplanMapper.updateById(teachplan);
        }else{
            //取出同父同级别的课程计划数量
            int count = getTeachplanCount(teachplanDto.getCourseId(), teachplanDto.getParentid());
            Teachplan teachplanNew = new Teachplan();
            //设置排序号
            teachplanNew.setOrderby(count+1);
            BeanUtils.copyProperties(teachplanDto,teachplanNew);

            teachplanMapper.insert(teachplanNew);

        }

    }

    @Override
    @Transactional
    public void detTeachplan(Long id) {

        //先查出是否是大章节
        Teachplan teachplan = teachplanMapper.selectById(id);
        if (teachplan.getParentid() == 0){
            LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.select(Teachplan::getId).eq(Teachplan::getParentid, id);
            List<Map<String, Object>> teachplanIdList = teachplanMapper.selectMaps(queryWrapper);
            if (teachplanIdList.isEmpty()){
                //是大章节 删除大章节，大单节下没有小章节时可以正常删除。
                teachplanMapper.deleteById(id);
            }else {
                //删除大章节，大章节下有小章节时不允许删除。
                throw new XueChengPlusException("120409","课程计划信息还有子级信息，无法操作");
            }
        }else {
            //删除小章节，同时将关联的信息进行删除。
            teachplanMapper.deleteById(id);
            LambdaQueryWrapper<TeachplanMedia> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(TeachplanMedia::getTeachplanId,teachplan.getId());
            teachplanMediaMapper.delete(queryWrapper);
        }


    }

    /**
     * @deprecated 课程计划排序
     * @param type 类型（上、下）
     * @param id
     */
    @Override
    public void moveDownOrMoveUp(String type, Long id) {
        //先查出该课程计划的排序等级
        Teachplan teachplan = teachplanMapper.selectById(id);

        if (type.equals("movedown")){
            //再查出该课程计划的最大排序
            LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Teachplan::getParentid,teachplan.getParentid()).orderByDesc(Teachplan::getOrderby).last("limit 1");

            Teachplan selectOne = teachplanMapper.selectOne(queryWrapper);
            if (teachplan.getOrderby().equals(selectOne.getOrderby())) return;

            //查出需要换的id
            LambdaQueryWrapper<Teachplan> downWrapper = new LambdaQueryWrapper<>();
            downWrapper.eq(Teachplan::getCourseId,teachplan.getCourseId()).eq(Teachplan::getOrderby,teachplan.getOrderby() + 1).eq(Teachplan::getParentid,teachplan.getParentid());
            Teachplan downTeachplan = teachplanMapper.selectOne(downWrapper);
            //向下移动
            downTeachplan.setOrderby(teachplan.getOrderby());
            teachplan.setOrderby(teachplan.getOrderby() + 1);
            teachplanMapper.updateById(teachplan);
            teachplanMapper.updateById(downTeachplan);

        }else if(type.equals("moveup")){
            if (teachplan.getOrderby() == 1) return;
            LambdaQueryWrapper<Teachplan> upWrapper = new LambdaQueryWrapper<>();
            upWrapper.eq(Teachplan::getCourseId,teachplan.getCourseId()).eq(Teachplan::getOrderby,teachplan.getOrderby() - 1).eq(Teachplan::getParentid,teachplan.getParentid());
            Teachplan upTeachplan = teachplanMapper.selectOne(upWrapper);
            //向上移动
            upTeachplan.setOrderby(teachplan.getOrderby());
            teachplan.setOrderby(teachplan.getOrderby() - 1);
            teachplanMapper.updateById(teachplan);
            teachplanMapper.updateById(upTeachplan);
        }
    }

    /**
     * @description 获取最新的排序号
     * @param courseId  课程id
     * @param parentId  父课程计划id
     * @return int 最新排序号
     * @author Mr.M
     * @date 2022/9/9 13:43
     */
    private int getTeachplanCount(long courseId,long parentId){
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getCourseId,courseId);
        queryWrapper.eq(Teachplan::getParentid,parentId);
        Integer count = teachplanMapper.selectCount(queryWrapper);
        return count;
    }


}
